'use strict'
exports.main = async (event, context) => {
  console.log('云函数内：event', event)

  if (event.access_token && event.openid) {
    let res = await uniCloud.getPhoneNumber({
      appid: event.APPID,
      provider: 'univerify',
      // apiKey: '', // 在uniCloud控制台开通一键登录服务并获取apiKey
      // apiSecret: '', // 在uniCloud控制台开通一键登录服务并获取apiSecret
      access_token: event.access_token || '',
      openid: event.openid || '',
    })

    if (res.success) {
      const mobile = res.phoneNumber

      try {
        let params = {
          mobile: mobile,
          type: 2,
        }
        params.registration_id = event.registration_id
        params.nickname = event.nickname
        params.identity_type = event.identity_type
        params.identity_status = event.identity_status
        params.equipment = event.equipment
        params.avatar = event.avatar
        params.is_download_app = event.is_download_app
        params.source = event.source
        const data = await uniCloud.httpclient.request(event.url, {
          method: 'POST',
          data: params,
          dataType: 'json',
        })
        console.log(params)
        return data.data
      } catch (e) {
        return e
      }
    } else {
      return {
        code: 400,
        message: res,
      }
    }
  } else {
    return {
      code: 400,
      message: 'access_token或openid不能为空',
    }
  }
}
